#include <stdio.h>
#include <stdlib.h>
#include "aimage.h"
#include "aimage_utils.h"
#include "aimage_ppm.h"

#ifdef GONE
int get_ratio( ASTAR_DATA stars[], int num_stars, float &avg_ratio )
{
	float ratio_total = 0;
	float ratio_count = 0;
	int i;

	for ( i = 0; i < num_stars; ++i )
	{
		//if ( stars[ i ].minrad > 5 )
		if ( stars[ i ].pixels > 300 )
		{
			float ratio;
			ratio = stars[ i ].minrad / stars[ i ].maxrad;
			ratio_total += ratio;
			ratio_count += 1;
		}
	}

	avg_ratio = ratio_total / ratio_count;

	return ratio_count+.5;
}
#endif

int main( int argc, const char *argv[] )
{
	AIMAGE image;
	AIMAGE image2;
	int width, height;
	float best_sample=-1;
	int best_shift=-1;

	if ( argc != 1 && argc != 2 )
	{
		fprintf(stderr, "%s [in_file]\n", argv[0] );
		exit( 1 );
	}

	if ( argc == 2 )
	{
		fprintf(stderr, "reading image %s\n", argv[1] );
		AIMAGE_PPM_UTILS::load( image, argv[1] );
	}
	else
	{
		AIMAGE_PPM_UTILS::load( image, "stdin" );
	}

	fprintf(stderr, "copy image\n");
	image.get_dim( width, height );
	image2.alloc_image( width, height );
	AIU::cls( image2, FULL_INT, 0, 0 );

	ASTAR_FIELD orig_stars;
	fprintf(stderr, "   copy image\n");
	image2.copy( image, 0, 0, width, height, 0, 0 );
	fprintf(stderr, "   find stars\n");
	image2.starfind( .20, orig_stars );
	fprintf(stderr, "   filter by distortion\n");
	orig_stars.filter_by_distortion( .5 );
	fprintf(stderr, "   filter to pixels\n");
	orig_stars.top_x_by_pixels( 20 );
	best_sample = orig_stars.get_ratio();
	ASTAR_FIELD best_stars;
	best_stars = orig_stars;
	fprintf(stderr, "Orig sample %g stars %d\n", best_sample,
		orig_stars.get_num_stars());

	int b;
	for ( b = 1; b < 10; ++b )
	{
		ASTAR_FIELD stars;
		fprintf(stderr, "Red/ Blueshift %d\n", b );
		image2.copy( image, 0, 0, width, height, 0, 0 );
		image2.blueshift( b );
		image2.redshift( -b );
		int stars_found = image2.starfind( .20, stars );
		float ratio, ratio_count;
		
		stars.map_to( orig_stars );
		stars.filter_out_unmapped();

		ratio = stars.get_ratio();
		ratio_count = stars.get_num_stars();
		fprintf(stderr, "%d stars found\n", stars_found );
		fprintf( stderr, "Avg Ratio is %g on %g samples\n", ratio, ratio_count );
		if ( ratio >= best_sample )
		{
			best_sample = ratio;
			best_shift = b;
			best_stars = stars;
			fprintf(stderr, "New best point found\n");
		}
	}

	fprintf( stderr, "Using shift %d \n", best_shift );

	image2.copy( image, 0, 0, width, height, 0, 0 );
	image2.blueshift( best_shift );
	image2.redshift( -best_shift );

#ifdef TODO
	int i;

	for ( i = 0; i < stars_found; ++i )
	{
		APIXEL red, green, white;

		red.r = FULL_INT; red.g = 0; red.b = 0;
		green.r = 0; green.g = FULL_INT; green.b = 0;
		white.r = FULL_INT; white.g = FULL_INT; white.b = FULL_INT;

		image2.circle( stars[ i ].xc, stars[ i ].yc, stars[i].minrad, red ); 
		image2.circle( stars[ i ].xc, stars[ i ].yc, stars[i].maxrad, green); 

		image2.setpixel( stars[i].xc, stars[i].yc, white );

	}
#endif

	//image2.drawstars( best_stars ); 
	//image2.drawstars( orig_stars ); 

	fprintf(stderr, "writing image\n");
	AIMAGE_PPM_UTILS::save( image2, "stdout", false );
}

